import java.sql.ResultSet;
import java.sql.SQLException;

/**
 *
 * @author Andy
 */
public class GamePreface extends javax.swing.JPanel {
    private DatabaseConnection dbConnect;
    private Controller controls;
    /**
     * Creates new form GamePreface
     */
    public GamePreface(Controller control, DatabaseConnection db) {
        initComponents();
        controls = control;
        dbConnect = db;
    }
    
    /**
       Finds three random words for the game from the database 
    **/
    public void getNewWords() {
        dbConnect.connect();
        
        String randomWordQuery = "SELECT word AS word FROM words w, (SELECT FLOOR(MAX(w1.id) * RAND())"
            + " AS randId FROM words w1) AS someRandId WHERE w.id = someRandId.randId";
        try {
            ResultSet set1 = dbConnect.executeQuery(randomWordQuery);
            set1.next();
            this.oneCoinWord.setText(set1.getString("word"));
            ResultSet set2 = dbConnect.executeQuery(randomWordQuery);
            set2.next();
            this.twoCoinWord.setText(set2.getString("word"));
            ResultSet set3 = dbConnect.executeQuery(randomWordQuery);
            set3.next();
            this.threeCoinWord.setText(set3.getString("word"));
        }
        catch (SQLException e) {
            System.err.println("Problem retrieving words from the database!");
        }
        finally {
            dbConnect.closeConnection();
        }
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        bannerPanel = new javax.swing.JPanel();
        bannerLabel = new javax.swing.JLabel();
        jPanel1 = new javax.swing.JPanel();
        coin1Label = new javax.swing.JLabel();
        oneCoinWord = new javax.swing.JLabel();
        oneCoinButton = new javax.swing.JButton();
        jPanel2 = new javax.swing.JPanel();
        coin1Label1 = new javax.swing.JLabel();
        twoCoinWord = new javax.swing.JLabel();
        coin1Label2 = new javax.swing.JLabel();
        twoCoinButton = new javax.swing.JButton();
        jPanel3 = new javax.swing.JPanel();
        coin1Label3 = new javax.swing.JLabel();
        threeCoinWord = new javax.swing.JLabel();
        coin1Label4 = new javax.swing.JLabel();
        coin1Label5 = new javax.swing.JLabel();
        threeCoinButton = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();

        setBackground(new java.awt.Color(249, 249, 252));
        setPreferredSize(new java.awt.Dimension(720, 650));

        bannerPanel.setBackground(new java.awt.Color(249, 249, 252));
        bannerPanel.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));

        bannerLabel.setHorizontalAlignment(javax.swing.SwingConstants.TRAILING);
        bannerLabel.setIcon(new javax.swing.ImageIcon(getClass().getResource("/draw-something-logo.jpg"))); // NOI18N
        bannerLabel.setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));

        javax.swing.GroupLayout bannerPanelLayout = new javax.swing.GroupLayout(bannerPanel);
        bannerPanel.setLayout(bannerPanelLayout);
        bannerPanelLayout.setHorizontalGroup(
            bannerPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(bannerPanelLayout.createSequentialGroup()
                .addComponent(bannerLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 576, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(0, 0, Short.MAX_VALUE))
        );
        bannerPanelLayout.setVerticalGroup(
            bannerPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(bannerLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 95, Short.MAX_VALUE)
        );

        jPanel1.setBackground(new java.awt.Color(252, 252, 252));

        coin1Label.setIcon(new javax.swing.ImageIcon(getClass().getResource("/coin.png"))); // NOI18N

        oneCoinWord.setFont(new java.awt.Font("Tahoma", 0, 18)); // NOI18N
        oneCoinWord.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        oneCoinWord.setToolTipText("");

        oneCoinButton.setText("Choose Me!");
        oneCoinButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                oneCoinButtonActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                .addGap(44, 44, 44)
                .addComponent(oneCoinButton, javax.swing.GroupLayout.PREFERRED_SIZE, 112, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(oneCoinWord, javax.swing.GroupLayout.PREFERRED_SIZE, 283, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(coin1Label)
                .addGap(199, 199, 199))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                .addContainerGap(37, Short.MAX_VALUE)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                    .addComponent(oneCoinButton, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                        .addComponent(oneCoinWord, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(coin1Label, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
                .addGap(28, 28, 28))
        );

        jPanel2.setBackground(new java.awt.Color(252, 252, 252));

        coin1Label1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/coin.png"))); // NOI18N

        twoCoinWord.setFont(new java.awt.Font("Tahoma", 0, 18)); // NOI18N
        twoCoinWord.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        twoCoinWord.setToolTipText("");

        coin1Label2.setIcon(new javax.swing.ImageIcon(getClass().getResource("/coin.png"))); // NOI18N

        twoCoinButton.setText("Choose Me!");
        twoCoinButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                twoCoinButtonActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel2Layout.createSequentialGroup()
                .addGap(31, 31, 31)
                .addComponent(twoCoinButton, javax.swing.GroupLayout.PREFERRED_SIZE, 112, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(twoCoinWord, javax.swing.GroupLayout.PREFERRED_SIZE, 283, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(coin1Label1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(coin1Label2)
                .addGap(134, 134, 134))
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel2Layout.createSequentialGroup()
                .addContainerGap(37, Short.MAX_VALUE)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                    .addComponent(twoCoinWord, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(coin1Label1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(coin1Label2, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(twoCoinButton, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addGap(28, 28, 28))
        );

        jPanel3.setBackground(new java.awt.Color(252, 252, 252));

        coin1Label3.setIcon(new javax.swing.ImageIcon(getClass().getResource("/coin.png"))); // NOI18N

        threeCoinWord.setFont(new java.awt.Font("Tahoma", 0, 18)); // NOI18N
        threeCoinWord.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        threeCoinWord.setToolTipText("");

        coin1Label4.setIcon(new javax.swing.ImageIcon(getClass().getResource("/coin.png"))); // NOI18N

        coin1Label5.setIcon(new javax.swing.ImageIcon(getClass().getResource("/coin.png"))); // NOI18N

        threeCoinButton.setText("Choose Me!");
        threeCoinButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                threeCoinButtonActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
        jPanel3.setLayout(jPanel3Layout);
        jPanel3Layout.setHorizontalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel3Layout.createSequentialGroup()
                .addGap(44, 44, 44)
                .addComponent(threeCoinButton, javax.swing.GroupLayout.PREFERRED_SIZE, 105, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(threeCoinWord, javax.swing.GroupLayout.PREFERRED_SIZE, 283, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 37, Short.MAX_VALUE)
                .addComponent(coin1Label3)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(coin1Label4)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(coin1Label5)
                .addGap(91, 91, 91))
        );
        jPanel3Layout.setVerticalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel3Layout.createSequentialGroup()
                .addContainerGap(37, Short.MAX_VALUE)
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                    .addComponent(threeCoinWord, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(coin1Label3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(coin1Label4, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(coin1Label5, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(threeCoinButton, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addGap(28, 28, 28))
        );

        jLabel1.setFont(new java.awt.Font("Tahoma", 0, 36)); // NOI18N
        jLabel1.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jLabel1.setText("Choose a word to draw!");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(bannerPanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addComponent(jPanel3, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(107, 107, 107)
                        .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 495, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(0, 0, Short.MAX_VALUE)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(bannerPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 55, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(jPanel3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(148, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void oneCoinButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_oneCoinButtonActionPerformed
        String word = this.oneCoinWord.getText();
        this.controls.game.selectWord(word, 1);
    }//GEN-LAST:event_oneCoinButtonActionPerformed

    private void twoCoinButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_twoCoinButtonActionPerformed
        String word = this.twoCoinWord.getText();
        this.controls.game.selectWord(word, 2);
    }//GEN-LAST:event_twoCoinButtonActionPerformed

    private void threeCoinButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_threeCoinButtonActionPerformed
        String word = this.threeCoinWord.getText();
        this.controls.game.selectWord(word, 3);
    }//GEN-LAST:event_threeCoinButtonActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel bannerLabel;
    private javax.swing.JPanel bannerPanel;
    private javax.swing.JLabel coin1Label;
    private javax.swing.JLabel coin1Label1;
    private javax.swing.JLabel coin1Label2;
    private javax.swing.JLabel coin1Label3;
    private javax.swing.JLabel coin1Label4;
    private javax.swing.JLabel coin1Label5;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JButton oneCoinButton;
    private javax.swing.JLabel oneCoinWord;
    private javax.swing.JButton threeCoinButton;
    private javax.swing.JLabel threeCoinWord;
    private javax.swing.JButton twoCoinButton;
    private javax.swing.JLabel twoCoinWord;
    // End of variables declaration//GEN-END:variables
}
